<?php
/*
 The deleted version was commented, now I'm too lazy to do this
*/
class Head {
	private $tags;

	private function addTemp($array, $key, $val) {
		$new = $array;
		$new[$key] = $val;
		return $new;
	}
	
	function __construct($tags=array()) {
		$this->tags = $tags;
	}

	function getByTag($tag) {
		return array_filter($this->tags, create_function('$a', "if (\$a[0]=='$tag') return \$a;"));
	}

	function countTags($tag) { return count($this->getByTag($tag)); }

	function add($tag, $params=array(), $content='', $ifie=false, $priority=5) {
		if ($this->countTags('title')>0 && $tag=='title') $this->setTitle($content);
		else array_push($this->tags, array(strtolower($tag), $params, $content, $ifie, $priority));
	}

	function addArray($arr) {
		$this->tags = array_merge($this->tags, $arr);
	}
	
	// Usage: addScript('firstScript.js', 'IE[forie.js]', '7 IE 7[forie7.js]' // Priotiry 7
	//, 'IE lt 7[forielessthan7.js]')
	function addScript() {
		$args = func_get_args();
	    $toPrint = array('type' => 'text/javascript');
		foreach($args as $entry) {	
			preg_match('/(\d)?\s?(?:(IE\s?[^\[]*)\s?)?\[?([^\]]*).*/', $entry, $preg);
			if($preg[2]=='')$preg[2]=false;
			if($preg[1]=='')$preg[1]=5;
			$this->add('script', $this->addTemp($toPrint, 'src', $preg[3]), '', $preg[2], $preg[1]); 
		}
	}
	
	// Usage: addCSS('sryle.css', 'IE[forie.css]', 'IE 7[forie7.css]', 'IE lt 7[forielessthan7.css]')
	function addCSS() {
		$args = func_get_args();
	    $toPrint = array('rel'=>'stylesheet','type'=>'text/css');
		foreach($args as $entry) {	
			preg_match('/(\d)?\s?(?:(IE\s?[^\[]*)\s?)?\[?([^\]]*).*/', $entry, $preg);
			if($preg[2]=='')$preg[2]=false;
			if($preg[1]=='')$preg[1]=5;
			$this->add('link', $this->addTemp($toPrint, 'href', $preg[3]), '', $preg[2], $preg[1]); 
		}
	}
	
	function setTitle($title) {
		if ($this->countTags('title')<1) $this->add('title', array(), $title);
		else array_walk($this->tags, create_function('&$el, $k, $t', "if (\$el[0]=='title') \$el[2]=\$t;"), $title);
	}

	function tags() { return $this->tags; }

	function sortHeads($a, $b) {
		if($a[0]=='meta')return -1;
		if($b[0]=='meta')return 1;
		$res = strcmp($a[0], $b[0]);
		if($res) return $res; else
		if($a[4]==$b[4]) return 0; else return ($a[4]>$b[4]);
	}
	
	function write($return=false) {
		$code = '';
		usort($this->tags, array("Head", "sortHeads"));
		while (list($k, $v) = each($this->tags)) {
			$tag     = $v[0];
			$params  = $v[1];
			$content = $v[2];
			$ifie 	 = $v[3];
			$code.='    ';
			if ($ifie) {
				if (is_bool($ifie)) $ver = 'IE';                                // <!--[if IE]> OR
				else $ver = ' '.$ifie;                                          // <!--[if IE CONDITION]>
				$code .= '<!--[if '.$ver.']>';
			}
			$code .= "<{$tag}";                                             // <tag
			while (list($par, $val) = each($params)) $code .= " $par=\"$val\""; //  param="value"
			if (empty($content) && ($tag!='script')) $code .= " />";            //  /> OR
			else $code .= ">$content</$tag>";                                   // >content</tag>
			if ($ifie) $code .= '<![endif]-->';                                 // <![endif]-->
			$code .= "\n";
		}
		if ($return) return $code;
		else echo $code;
	}
}
?>
